package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList){
        System.out.println("新增图书！");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入图书的名字：");
        String name = scanner.nextLine();
        System.out.println("请输入图书的作者:");
        String author = scanner.nextLine();
        System.out.println("请输入图书的类型:");
        String type = scanner.nextLine();
        System.out.println("请输入图书的价格:");
        int price = scanner.nextInt();

        Book book = new Book(name,author,type,price);

        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            //如果数组中已有该书则新增失败！
            Book tempBook = bookList.getBook(i);
            if(tempBook.getName().equals(name)&&tempBook.getAuthor().equals(author)){
                System.out.println("该书已存在！新增失败！");
                return;
            }
        }
        System.out.println("请输入新增图书位置：（下标）");
        int pos = scanner.nextInt();
        if(pos<0||pos> bookList.getUsedSize()||pos>=BookList.DEFAULT_SIZE) {
            System.out.println("位置非法!操作失败！");
            return;
        }
        bookList.setBook(pos,book);

        bookList.setUsedSize(currentSize+1);

        System.out.println("新增成功！");


    }
}
